import prisma from '../src/lib/prisma'
import { hash } from 'bcryptjs'

async function main() {
  console.log('开始创建测试数据...')

  // 创建教师账号
  const teacher = await prisma.user.upsert({
    where: { email: 'teacher@example.com' },
    update: {},
    create: {
      email: 'teacher@example.com',
      name: '张老师',
      password: await hash('teacher123', 10),
      role: 'TEACHER',
    },
  })
  console.log('创建教师账号:', teacher.email)

  // 创建学生账号
  const students = []
  for (let i = 1; i <= 10; i++) {
    const student = await prisma.user.upsert({
      where: { email: `student${i}@example.com` },
      update: {},
      create: {
        email: `student${i}@example.com`,
        name: `学生${i}`,
        password: await hash('student123', 10),
        role: 'STUDENT',
      },
    })
    students.push(student)
    console.log('创建学生账号:', student.email)
  }

  // 创建一个示例实验
  const experiment = await prisma.experiment.create({
    data: {
      title: '数据结构实验一：链表操作',
      description: '本实验要求学生实现链表的基本操作，包括插入、删除、查找等功能。',
      startTime: new Date(),
      endTime: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30天后
      createdById: teacher.id,
      stages: {
        create: [
          {
            title: '链表基础知识测试',
            description: '测试学生对链表基本概念的理解',
            order: 1,
            type: 'MULTIPLE_CHOICE',
            content: JSON.stringify({
              questions: [
                {
                  question: '链表的时间复杂度是？',
                  options: ['O(1)', 'O(n)', 'O(log n)', 'O(n²)'],
                  answer: 1
                }
              ]
            })
          },
          {
            title: '链表实现',
            description: '实现一个单向链表',
            order: 2,
            type: 'CODE',
            content: JSON.stringify({
              requirements: '实现一个包含插入、删除、查找功能的单向链表'
            })
          }
        ]
      }
    }
  })
  console.log('创建示例实验:', experiment.title)

  // 授权前5个学生访问实验
  for (let i = 0; i < 5; i++) {
    await prisma.experimentAuthorization.create({
      data: {
        experimentId: experiment.id,
        studentId: students[i].id
      }
    })
    console.log(`授权学生${i + 1}访问实验`)
  }

  console.log('测试数据创建完成！')
  console.log('\n可以使用以下账号登录：')
  console.log('教师账号: teacher@example.com / teacher123')
  console.log('学生账号: student1@example.com / student123')
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  }) 